package com.gzist.elderlyserviceplatform.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;


/**
 * Swagger3配置类
 */
@Configuration
public class Swagger3Config implements WebMvcConfigurer {

    // 基础信息配置
    private Info commonApiInfo(String description) {
        return new Info()
                .title("养老服务整合平台-Knife4j-API文档")
                .description(description)
                .version("1.0")
                .contact(new Contact()
                        .name("hzj")
                        .url("https://blog.csdn.net/m0_74958221?spm=1000.2115.3001.5343 ")
                        .email("2065501873@qq.com"));
    }

    // 用户端接口配置
    @Bean
    public GroupedOpenApi webApi() {
        return GroupedOpenApi.builder()
                .group("用户端接口")
                .pathsToMatch("/api/client/**")
                .packagesToScan("com.gzist.elderlyserviceplatform.controller")
                .addOpenApiCustomizer(openApi -> {
                    openApi.setInfo(commonApiInfo("用户端的接口"));
                    // 添加全局参数（替换原有ParameterBuilder方式）
                    openApi.getComponents().addParameters("userId",
                            new io.swagger.v3.oas.models.parameters.Parameter()
                                    .name("userId")
                                    .description("用户token")
                                    .in("header")
                                    .required(false)
                                    .schema(new io.swagger.v3.oas.models.media.Schema<>().type("string"))
                                    .example("1"));
                })
                .build();
    }

    // 管理端接口配置
    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("后台接口")
                .pathsToMatch("/api/admin/**")
                .packagesToScan("com.gzist.elderlyserviceplatform.controller")
                .addOpenApiCustomizer(openApi -> {
                    openApi.setInfo(commonApiInfo("管理端的接口"));
                    openApi.getComponents().addParameters("adminId",
                            new io.swagger.v3.oas.models.parameters.Parameter()
                                    .name("adminId")
                                    .description("用户token")
                                    .in("header")
                                    .required(false)
                                    .schema(new io.swagger.v3.oas.models.media.Schema<>().type("string"))
                                    .example("1"));
                })
                .build();
    }

    // 公共接口配置
    @Bean
    public GroupedOpenApi commonApi() {
        return GroupedOpenApi.builder()
                .group("公共接口")
                .pathsToMatch("/api/common/**")
                .packagesToScan("com.gzist.elderlyserviceplatform.controller")
                .addOpenApiCustomizer(openApi -> {
                    openApi.setInfo(commonApiInfo("公共接口"));
                })
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}